
<template>
  <el-dialog v-model="isShowDialog"
             :before-close="handleClose"
             :title="title"
             draggable
             width="500px"
  >
    <el-form  ref="formRef" :disabled="isView" :model="saveParams" label-width="100px" :rules="rules">
      <el-form-item label="上级分类">
        <el-input v-model="saveParams.parentTitle" disabled clearable/>
      </el-form-item>
      <el-form-item label="分类名称" prop="title">
        <el-input v-model="saveParams.title" placeholder="分类名称" clearable/>
      </el-form-item>

      <el-form-item label="所属仓库" prop="warehouseId">
        <el-select
            :disabled="saveParams.classifyId"
            clearable
            v-model="saveParams.warehouseId"
        >
          <template v-for="(item,index) in warehouseList" :key="index">
            <el-option :label="item.warehouseName" :value="item.warehouseId"></el-option>
          </template>
        </el-select>
      </el-form-item>

      <el-form-item label="状态">
        <el-select
            :disabled="isView"
            style="width: 100%;"
            clearable
            placeholder="状态"
            v-model="saveParams.status"
        >
          <el-option label="启用" :value="0"></el-option>
          <el-option label="停用" :value="1"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
        <span class="dialog-footer">
          <el-button type="primary" @click="clickSave"> 确定 </el-button>
          <el-button @click="handleClose">取消</el-button>
        </span>
    </template>
  </el-dialog>
</template>

<script setup>
import {getCurrentInstance, onMounted, ref} from "vue";
import {skuClassifySave} from "@/api/scApi";
import {getCompanySelect, getWarehouseSelect} from "@/api/system/user";

const {proxy} = getCurrentInstance();
const props = defineProps({
  isShowDialog: {
    type: Boolean,
    default: false
  },
  title: {type: String},
  isView: {type: Boolean},
  parentData:{type:Object}, //新增用
  rowData:{type:Object}, //编辑用
})
let saveParams=ref({})
onMounted(()=>{
  getWarehouse().then(()=>{
    if(JSON.stringify(props.rowData) !='{}'){
      //编辑
      saveParams.value=JSON.parse(JSON.stringify(props.rowData))
    }else{
      //新增
      saveParams.value.parentId=props.parentData.classifyId
      saveParams.value.parentTitle=props.parentData.title
      saveParams.value.level=props.parentData.level
      saveParams.value.status=0
    }
  })

})

const emits = defineEmits(['cancel'])
function handleClose() {
  emits('cancel')
}

const rules = ref({
  title: [{required: true, message: "名称不能为空", trigger: "blur"}],
  warehouseId: [{required: true, message: "所属仓库不能为空", trigger: "change"}],
});

function clickSave(){
  proxy.$refs.formRef.validate((valid, fileds) => {
    if (valid) {

      skuClassifySave(saveParams.value).then(res => {
        if (res.code === 200) {
          proxy.$modal.msgSuccess('保存成功')
          emits('cancel', true)
        }
      })
    }
  })
}

//获取仓库下拉数据
let warehouseList = ref([])
const sysUserInfo=JSON.parse(localStorage.getItem("userInfo"))
function getWarehouse(){
  return new Promise((resolve, reject) => {
    getWarehouseSelect({'companyId': sysUserInfo.companyId}).then(res => {
      warehouseList.value = res.data
      resolve()
    })
  })
}

</script>

<style scoped lang="scss">

</style>